<?php

class User_Chat {
    const USER_CHAT = "user_v2_chat";
    const GROUP = "default";

    public static function getTouristChatByUid($uid,$limit=5,$offset=0){
        $count = DB::select(array(DB::expr('count(id)'),'counts'),'guiderId')
            ->from(self::USER_CHAT)
            ->where('uid','=',$uid)
            ->where('status','in',array(1,20))
            ->group_by('guiderId')
            ->order_by('created','desc')
            ->execute()
            ->count();

        $query = DB::select(array(DB::expr('count(id)'),'counts'),'guiderId')
            ->from(self::USER_CHAT)
            ->where('uid','=',$uid)
            ->where('status','in',array(1,20))
            ->group_by('guiderId')
            ->order_by('created','desc')
            ->limit($limit)
            ->offset($offset);

        $query = $query->execute();
        $list = array();
        $cache = Cache::instance('file');
        foreach($query as $v){

            $data = self::getChatDataByUidAndGuiderId($uid,$v['guiderId']);
            $key = $v['guiderId'].'-'.$uid;
            $unread = $cache->get($key);

            $user = user::getUserFromUid($v['guiderId']);
            $v['userIcon'] = $user->getUserIcon();
            $v['userName'] = $user->getUserName();
            $v['message'] = $data['message'];
            $v['created'] = date("Y.m.d H:i",$data['created']);
            $v['unread'] = $unread;
            $list[] = $v;
        }

        return array($count,$list);

    }


    public static function getTouristChatByUidAdmin($uid){
        $count = DB::select(array(DB::expr('count(id)'),'counts'),'guiderId')
            ->from(self::USER_CHAT)
            ->where('uid','=',$uid)
            ->where('status','in',array(1,20))
            ->group_by('guiderId')
            ->order_by('created','desc')
            ->execute()
            ->count();

        $query = DB::select(array(DB::expr('count(id)'),'counts'),'guiderId')
            ->from(self::USER_CHAT)
            ->where('uid','=',$uid)
            ->where('status','in',array(1,20))
            ->group_by('guiderId')
            ->order_by('created','desc')
          ;

        $query = $query->execute();
        $list = array();
        $cache = Cache::instance('file');
        foreach($query as $v){

            $data = self::getChatDataByUidAndGuiderId($uid,$v['guiderId']);
            $key = $v['guiderId'].'-'.$uid;
            $unread = $cache->get($key);

            $user = user::getUserFromUid($v['guiderId']);
            $v['userIcon'] = $user->getUserIcon();
            $v['userName'] = $user->getUserName();
            $v['message'] = $data['message'];
            $v['created'] = date("Y.m.d H:i",$data['created']);
            $v['unread'] = $unread;
            $list[] = $v;
        }

        return array($count,$list);

    }


    public static function getGuiderChatByUid($uid,$limit=5,$offset=0){
        $count = DB::select(array(DB::expr('count(id)'),'counts'),'uid')
            ->from(self::USER_CHAT)
            ->where('guiderId','=',$uid)
            ->where('status','in',array(1,10))
            ->group_by('uid')
            ->order_by('created','desc')
            ->execute()
            ->count();
        $query = DB::select(array(DB::expr('count(id)'),'counts'),'uid')
            ->from(self::USER_CHAT)
            ->where('guiderId','=',$uid)
            ->where('status','in',array(1,10))
            ->group_by('uid')
            ->order_by('created','desc') ->limit($limit)
            ->offset($offset);
        $cache = Cache::instance('file');

        $query = $query->execute();
        $list = array();
        foreach($query as $v){

            $data = self::getChatDataByUidAndGuiderId($v['uid'],$uid);
            $key = $v['uid'].'-'.$uid;
            $unread = $cache->get($key);

            $user = user::getUserFromUid($v['uid']);
            $v['userIcon'] = $user->getUserIcon();
            $v['userName'] = $user->getUserName();
            $v['message'] = $data['message'];
            $v['created'] = date("Y.m.d H:i",$data['created']);
            $v['unread'] = $unread;
            $list[] = $v;
        }

        return array($count,$list);
    }

    public static function getGuiderChatByUidAdmin($uid){
        $count = DB::select(array(DB::expr('count(id)'),'counts'),'uid')
            ->from(self::USER_CHAT)
            ->where('guiderId','=',$uid)
            ->where('status','in',array(1,10))
            ->group_by('uid')
            ->order_by('created','desc')
            ->execute()
            ->count();
        $query = DB::select(array(DB::expr('count(id)'),'counts'),'uid')
            ->from(self::USER_CHAT)
            ->where('guiderId','=',$uid)
            ->where('status','in',array(1,10))
            ->group_by('uid')
            ->order_by('created','desc')
         ;
        $cache = Cache::instance('file');

        $query = $query->execute();
        $list = array();
        foreach($query as $v){

            $data = self::getChatDataByUidAndGuiderId($v['uid'],$uid);
            $key = $v['uid'].'-'.$uid;
            $unread = $cache->get($key);

            $user = user::getUserFromUid($v['uid']);
            $v['userIcon'] = $user->getUserIcon();
            $v['userName'] = $user->getUserName();
            $v['message'] = $data['message'];
            $v['created'] = date("Y.m.d H:i",$data['created']);
            $v['unread'] = $unread;
            $list[] = $v;
        }

        return array($count,$list);
    }

    //游客编号，导游编号
    public static function getChatDataByUidAndGuiderId($uid,$guiderId){
        return DB::select()
            ->from(self::USER_CHAT)
            ->where('uid','=',$uid)
            ->where('guiderId','=',$guiderId)
            ->order_by('created','desc')
            ->execute()
            ->current();
    }

    public static function removeTouristChatByGuiderId($id){
        $query = DB::select()
            ->from(self::USER_CHAT)
            ->where('guiderId', '=', $id)
            ->where('status','!=',0)
            ->execute();
        $rows = 0;
        foreach($query as $v){
            if($v['status'] == 1){
                $data = array('status'=>10);
                $rows = DB::update('user_v2_chat')
                    ->set($data)
                    ->where('id', '=', $v['id'])
                    ->execute(self::GROUP);
            }else{
                $data = array('status'=>0);
                $rows = DB::update('user_v2_chat')
                    ->set($data)
                    ->where('id', '=', $v['id'])
                    ->execute(self::GROUP);
            }
        }
        return $rows;
//        $data = array('status'=>0);
//        $rows = DB::update('user_v2_chat')
//            ->set($data)
//            ->where('guiderId', '=', $id)
//            ->execute(self::GROUP);
//        return $rows;
    }

    public static function removeGuiderChatByUid($id){
        $query = DB::select()
            ->from(self::USER_CHAT)
            ->where('uid','=',$id)
            ->where('status','!=',0)
            ->execute();
        $rows = 0;
        foreach($query as $v){
            if($v['status'] == 1){
                $data = array('status'=>20);
                $rows = DB::update('user_v2_chat')
                    ->set($data)
                    ->where('id', '=', $v['id'])
                    ->execute(self::GROUP);
            }else{
                $data = array('status'=>0);
                $rows = DB::update('user_v2_chat')
                    ->set($data)
                    ->where('id', '=', $v['id'])
                    ->execute(self::GROUP);
            }
        }
        return $rows;
    }

}